#include "string.hpp"

namespace canoe
{
	const size_t string::npos = -1;
	std::ostream& operator<<(std::ostream& cout, const string& s)
	{
		//cout << s._str;
		//return cout;
		for (size_t i = 0; i < s._size; i++)
		{
			cout << s[i];
		}
		return cout;
	}
	std::istream& operator>>(std::istream& cin, string& s)
	{
		//cin >> s._str;
		//return cin;
		s.clear();
		char ch = cin.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			//cin >> ch;
			ch = cin.get();
		}
		return cin;
	}
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];
		memcpy(_str, str, _size + 1);
	}
	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		memcpy(_str, s._str, s._size + 1);
		_size = s._size;
		_capacity = s._capacity;
	}
	string& string::operator=(const string& s)
	{
		string tmp(s);
		swap(tmp);
		return *this;
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}
	void string::push_back(char c)
	{
		if (_size >= _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}
		_str[_size++] = c;
		_str[_size] = '\0';
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}
	void string::append(const char* str)
	{
		assert(str != nullptr);
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			size_t newcapacity = 2 * _capacity > _size + len ? 2 * _capacity : _size + len;
			reserve(newcapacity);
		}
		memcpy(_str + _size, str, len + 1);
		_size += len;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	const char* string::c_str() const
	{
		return _str;
	}
	size_t string::size() const
	{
		return _size;
	}
	size_t string::capacity() const
	{
		return _capacity;
	}
	bool string::empty() const
	{
		return _size == 0;
	}
	void string::resize(size_t n, char c)
	{
		if (n > _size)
		{
			if (n > _capacity)
				reserve(n);
			for (size_t i = _size; i < n; i++)
				_str[i] = c;
			_size = n;
		}
		else
		{
			_size = n;
		}
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* str = new char[n + 1];
			memcpy(str, _str, _size + 1);
			delete[] _str;
			_str = str;
			_capacity = n;
		}
	}
	char& string::operator[](size_t index)
	{
		assert(index <= _size);
		return _str[index];
	}
	const char& string::operator[](size_t index) const
	{
		assert(index <= _size);
		return _str[index];
	}
	bool string::operator<(const string& s) const
	{
		size_t i1 = 0, i2 = 0;
		while (i1 < _size && i2 < s._size)
		{
			if (_str[i1] < s[i2])
				return true;
			if (_str[i1] > s[i2])
				return false;
			++i1;
			++i2;
		}
		return i2 < s._size;
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s) const
	{
		return !(*this < s);
	}
	bool string::operator==(const string& s) const
	{
		size_t i1 = 0, i2 = 0;
		while (i1 < _size && i2 < s._size)
		{
			if (_str[i1] != s[i2])
				return false;
			++i1;
			++i2;
		}
		return true;
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}
	size_t string::find(char c, size_t pos) const
	{
		for (size_t i = 0; i < _size; i++)
		{
			if (_str[i] == c)
				return i;
		}
		return npos;
	}
	size_t string::find(const char* s, size_t pos) const
	{
		const char* p = strstr(_str + pos, s);
		if (p == nullptr)
			return npos;
		else
			return p - _str;
	}
	string& string::insert(size_t pos, char c)
	{
		assert(pos <= _size);
		if (_size >= _capacity)
			reserve(2 * _capacity);
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end--] = _str[end - 1];
		}
		_str[pos] = c;
		++_size;
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);

		if (pos + len > _capacity)
			reserve(2 * _capacity > _size + len ? 2 * _capacity : _size + len);

		size_t end = _size + len;

		while (end > pos + len - 1)
		{
			_str[end--] = _str[end - len];
		}
		
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
		
		_size += len;
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len == npos || len >= _size - pos)
		{
			_size = pos;
			_str[_size] = '\0';
		}
		else
		{
			size_t i = pos + len;
			memmove(_str + pos, _str + i, _size + 1 - i);
			_size -= len;
		}
		return *this;
	}
	string string::substr(size_t pos, size_t len) const
	{
		if (len == npos || len >= _size - pos)
			len = _size - pos;
		string ret;
		ret.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			ret += _str[pos + i];
		}
		return ret;
	}
}